package com.android.coordinatorlayoutview;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    TabLayout mTabLayout;
    private ImageView mExpand;
    private LinearLayout mLayoutMore;
    private RelativeLayout mRoot;
    private List<String> mTitles = new ArrayList<>();
    private ViewPager mViewpager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
    }

    private void initData() {
        mTitles.add("新闻");
        mTitles.add("财经");
        mTitles.add("娱乐");
        mTitles.add("体育");
        mTitles.add("军事");
        mTitles.add("科技");
        mTitles.add("教育");
        mTitles.add("历史");
        mTitles.add("文化");
        mTitles.add("深圳");
    }

    private void initView() {
        mTabLayout = (TabLayout) findViewById(R.id.tabs);
        mExpand = (ImageView) findViewById(R.id.Expand);
        mLayoutMore = (LinearLayout) findViewById(R.id.LayoutMore);
        mRoot = (RelativeLayout) findViewById(R.id.Root);
        mRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mLayoutMore.getVisibility() == View.VISIBLE) {
                    mLayoutMore.setVisibility(View.GONE);
                    mExpand.setImageResource(R.drawable.ic_page_statistic_monthly_expand);
                } else {
                    mLayoutMore.setVisibility(View.VISIBLE);
                    mExpand.setImageResource(R.drawable.ic_page_statistic_monthly_collapse);
                }
            }
        });
        mViewpager = (ViewPager) findViewById(R.id.viewpager);

        mTabLayout.setSelectedTabIndicatorHeight(mTitles.size());
        for (int i = 0; i < mTitles.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(mTitles.get(i)));
        }
        final List<Fragment> fragments = new ArrayList<Fragment>();
        for (int i = 0; i < mTitles.size(); i++) {
            TabFragment fragment = new TabFragment();
            Bundle bundle = new Bundle();
            bundle.putString(TabFragment.TITLE,mTitles.get(i));
            fragment.setArguments(bundle);
            fragment.setTitle(mTitles.get(i));
            fragments.add(fragment);
        }

        FragmentPagerAdapter pagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitles.get(position);
            }

        };
        //给ViewPager设置适配器
        mViewpager.setAdapter(pagerAdapter);
        //将TabLayout和ViewPager关联起来
        mTabLayout.setupWithViewPager(mViewpager);
    }


}
